function hFigure = subsasgn(hFigure, S, V)
% BVQXfigure::subsasgn  - set properties on objects
%
% FORMAT:       FigureObject.PropertyName = Value

% Version:      v0.8a
% Build:        9111303
% Date:         Nov-13 2009, 3:33 AM CET
% Author:       Jochen Weber, SCAN Unit, Columbia University, NYC, NY, USA
% URL / Info:   http://wiki.brainvoyager.com/BVQXtools

% class check
if nargin > 2 && ...
    ~isa(hFigure, 'BVQXfigure')
    try
        hFigure = builtin('subsasgn', hFigure, S, V);
    catch
        rethrow(lasterror);
    end
    return;
end

% argument check
if isempty(S)
    error( ...
        'BVQXfigure:BadSubsAsgn', ...
        'S struct may not be empty.' ...
    );
end
slen  = length(S);
stype = S(1).type;
ssubs = S(1).subs;

% decide on kind of subscripting, first struct-like notation
switch stype, case {'.'}
    
    % check for subscript type
    if ~ischar(ssubs) || ...
        isempty(ssubs)
        error( ...
            'BVQXfigure:BadSubsAsgn', ...
            'Struct notation needs a non-empty char property.' ...
        );
    end
    
    % only works for singular object
    if numel(hFigure) ~= 1
        error( ...
            'BVQXfigure:InvalidObjectSize', ...
            'Struct notation only works on singular objects.' ...
        );
    end
    
    % make content linear
    ssubs = ssubs(:)';
    
    % set complete property
    if slen == 1
        
        % try setting value
        try
            if strcmpi(get(hFigure.mhnd, 'Type'), 'axes') && ...
                strcmpi(ssubs, 'visible')
                set(get(hFigure.mhnd, 'Children'), 'Visible', V);
                BVQXfigure(hFigure, 'Set', 'Visible', V);
            else
                set(hFigure.mhnd, ssubs, V);
            end
        catch
            le = lasterror;
            if ~isempty(strfind(lower(le.identifier), 'invalidproperty'))
                try
                    set(get(hFigure.mhnd, 'Children'), ssubs, V);
                    if strcmpi(ssubs, 'cdata')
                        set(hFigure.mhnd, 'XLim', [0.5, 0.5 + size(V, 2)]);
                        set(hFigure.mhnd, 'YLim', [0.5, 0.5 + size(V, 1)]);
                    end
                    return;
                catch
                    % do nothing
                end
            end
            rethrow(lasterror);
        end
        
    % set sub value
    else
        
        % for TagStruct use alternative approach
        if strcmpi(ssubs, 'tagstruct')
            ts = BVQXfigure(hFigure, 'TagStruct');
            to = subsref(ts, S(2));
            subsasgn(to, S(3:end), V);
            return;
        end
        
        % try getting, altering and re-setting value
        try
            cV = get(hFigure.mhnd, ssubs);
            cV = subsasgn(cV, S(2:end), V);
            set(hFigure.mhnd, ssubs, cV);
        catch
            rethrow(lasterror);
        end
    end
    
% indexing requested
case {'()'}
    
    % we need non-empty cell subscript
    if ~iscell(ssubs) || ...
        isempty(ssubs)
        error( ...
            'BVQXfigure:BadSubsRef', ...
            'Can''t index into BVQXfigure matrix.' ...
        );
    end
    
    % convert hFigure to struct
    sFigure = struct(hFigure);
    
    % try to retrieve subscripted matrix
    try
        subset   = subsref(sFigure, S(1));
    catch
        error( ...
            'BQVXfigure:BadSubsRef', ...
            'Invalid subscript (%s).', ...
            lasterr ...
        );
    end
    
    % no further subsasgn requested
    if slen == 1
        if ~strcmpi(class(V), 'bvqxinifile')
            error( ...
                'BVQXfigure:BadSubsAsgnValue', ...
                'Class mismatch error.' ...
            );
        end
        
        % try to assign new objects into matrix
        try
            sFigure = subsasgn(sFigure, S(1), struct(V));
            hFigure = class(sFigure, 'BVQXfigure');
        catch
            error( ...
                'BVQXfigure:BadSubsAsgnIndex', ...
                'Couldn''t assign partial object matrix (%s).', ...
                lasterr ...
            );
        end
        return;
    end
    
    if numel(subset) ~= 1
        error( ...
            'BVQXfigure:InvalidObjectSize', ...
            'Further subscripting only works for singular objects.' ...
        );
    end
    
    try
        subsasgn(class(subset, 'BVQXfigure'), S(2:end), V);
    catch
        error( ...
            'BVQXfigure:BadSubsAsgnSubs', ...
            'Error passing subsasgn to object (%s).', ...
            lasterr ...
        );
    end
    
otherwise
    error( ...
        'BVQXfigure:BadSubsAsgn', ...
        'Only struct notation allowed to set values.' ...
    );
end
